package com.bhs.sansong.ui.order;

import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.OrderGoodsAdapter;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.EnquiryOrderRefresh;
import com.bhs.sansong.bean.OrderGoodsBean;
import com.bhs.sansong.bean.OrderGoodsDataBean;
import com.bhs.sansong.net.NetCallBack;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;
/**
 * 询价列表
 */
public class EnquiryOrderListFragment extends BaseFragment implements NetCallBack {

    private String type;
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvWaybill;
    private OrderGoodsAdapter mAdapter;
    private int page=1;

    public static EnquiryOrderListFragment newInstance(String type) {
        EnquiryOrderListFragment upstreamShipmentListFragment = new EnquiryOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        upstreamShipmentListFragment.setArguments(bundle);
        return upstreamShipmentListFragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_enquiry_order_list;
    }

    @Override
    public void initView(View root) {
        //注册监听
        EventBus.getDefault().register(this);
        type = getArguments().getString("type");
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvWaybill = root.findViewById(R.id.rv_waybill);
        mAdapter = new OrderGoodsAdapter(type);
        mRvWaybill.setAdapter(mAdapter);
        mRvWaybill.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void initData() {
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));

        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData("");
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData("");
            }
        }, mRvWaybill);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getContext(), OrderDetailsActivity.class)
                        .putExtra("deliver_sn", mAdapter.getData().get(position).getDeliver_sn()));
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OrderGoodsBean data = mAdapter.getData().get(position);
                if (view.getId() == R.id.btn_closing_inquiry) {
                    startActivity(new Intent(getActivity(), CancelOrderActivity.class)
                            .putExtra("deliver_sn", data.getDeliver_sn()));
                } else {
                    startActivity(new Intent(getActivity(), QuotationListActivity.class)
                            .putExtra("deliver_sn", data.getDeliver_sn())
                            .putExtra("startCity", data.getPickup_city())
                            .putExtra("endCity", data.getPickup_city()));
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        loadData("");
    }

    Map<String, String> map = new ArrayMap<>();

    private void loadData(String search_key) {
        map.put("search_type", type);
        map.put("search_key", search_key);
        map.put("page", String.valueOf(page));
        map.put("limit", "10");
        networkRequest(netApi.get_order_quoted_list(map), this, 111);
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        OrderGoodsDataBean bean = new Gson().fromJson(json, OrderGoodsDataBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        }
    }



    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }

    @Subscribe
    public void onEvent(EnquiryOrderRefresh event) {
        loadData(event.getText());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
